
#include <cstdio>
#include "member_BShader.h"

using namespace BWE;

member_BShader::member_BShader(BShader* shader)
{
	boss = shader;
	type = BShader::Type_None;
	object = 0;
	dirty = false;
}
member_BShader::~member_BShader()
{
	if (object)
	{
		//BOpenGL::release
	}
}

bool member_BShader::compile()
{
	if (text.empty())
		return false;
	if (dirty)
	{
		if (!object)
		{
			if (type == BShader::Type_Vertex)
				object = glCreateShader(GL_VERTEX_SHADER);
			if (type == BShader::Type_Fragment)
				object = glCreateShader(GL_FRAGMENT_SHADER);
		}
		if (object)
		{
			const char* cstr = text.cstr();
			glShaderSource(object, 1, &cstr, 0);
			if (!BOpenGL::Check())
				return false;
			glCompileShader(object);
			if (!BOpenGL::Check())
				return false;
			GLint status = 0;
			glGetShaderiv(object, GL_COMPILE_STATUS, &status);
			if (status == 0)
			{
				GLint length = 0;
				glGetShaderiv(object, GL_INFO_LOG_LENGTH, &length);
				log.reset('*', length);
				GLsizei len = 0;
				glGetShaderInfoLog(object, length, &len, (char*)log.cstr());
				printf("Comile Shader faild - %s : \n", boss->name().cstr());
				puts(log.cstr());
			}
		}
		dirty = false;
	}
	if (object)
		return true;
	return false;
}
